﻿// Type: JetBrains.Application.License.LicenseChecker
// Assembly: JetBrains.Platform.ReSharper.Shell, Version=6.0.2154.57, Culture=neutral, PublicKeyToken=1010a0d8d6380325
// Assembly location: D:\installed\JetBrains\ReSharper\v6.0\Bin\JetBrains.Platform.ReSharper.Shell.dll

using JetBrains.UI.Application.License;
using System;
using System.Text;

namespace JetBrains.Application.License
{
  public class LicenseChecker
  {
    private static BigInteger _zero = new BigInteger();
    private BigInteger N;
    private bool _hasLicense;
    private string _username;
    private string _company;
    private BigInteger _code;

    public bool HasLicense
    {
      get
      {
        return this._hasLicense;
      }
    }

    public bool IsChecksumOK
    {
      get
      {
        if (this._code != LicenseChecker._zero)
          return (this._code.IntValue() & (int) ushort.MaxValue) % 65521 == this.UserHash();
        else
          return false;
      }
    }

    public int Version
    {
      get
      {
        return this._code.IntValue() >> 16;
      }
    }

    public int Type
    {
      get
      {
        return (this._code >> 32).IntValue() & (int) byte.MaxValue;
      }
    }

    public DateTime GenerationDate
    {
      get
      {
        int num = (this._code >> 40).IntValue() & (int) ushort.MaxValue;
        try
        {
          return new DateTime(2003 + (num & (int) sbyte.MaxValue), num >> 7 & 15, num >> 11);
        }
        catch (Exception ex)
        {
          return DateTime.MinValue;
        }
      }
    }

    public DateTime ExpirationDate
    {
      get
      {
        int num = (this._code >> 56).IntValue() & (int) ushort.MaxValue;
        if (num == (int) ushort.MaxValue)
          return DateTime.MaxValue;
        try
        {
          return new DateTime(2003 + (num & (int) sbyte.MaxValue), num >> 7 & 15, num >> 11);
        }
        catch (Exception ex)
        {
          return DateTime.MinValue;
        }
      }
    }

    public int ProductVersion
    {
      get
      {
        return (this._code >> 72).IntValue() & (int) ushort.MaxValue;
      }
    }

    public int CustomerId
    {
      get
      {
        return (this._code >> 88).IntValue() & int.MaxValue;
      }
    }

    public int Edition
    {
      get
      {
        return (this._code >> 120).IntValue() & (int) ushort.MaxValue;
      }
    }

    static LicenseChecker()
    {
    }

    public LicenseChecker(string publickey, string username, string company, string license)
    {
      this._hasLicense = license.Length > 0;
      this.N = new BigInteger(publickey, 10);
      this._username = username.Trim();
      this._company = company;
      try
      {
        this._code = new BigInteger(Convert.FromBase64String(license));
      }
      catch (FormatException ex)
      {
        this._code = LicenseChecker._zero;
        return;
      }
      this._code = this._code.modPow(new BigInteger(LicenseChecker.GetBytesFromString(username)) | (BigInteger) 1, this.N);
    }

    private static byte[] GetBytesFromString(string s)
    {
      return Encoding.UTF8.GetBytes(s);
    }

    private int UserHash()
    {
      int num = 0;
      for (int index = 0; index < this._username.Length; ++index)
        num = ((num << 7) + (int) this._username[index]) % 65521;
      for (int index = 0; index < this._company.Length; ++index)
        num = ((num << 7) + (int) this._company[index]) % 65521;
      return num;
    }
  }
}
